package mhl.view;

import mhl.domain.Bill;
import mhl.domain.Menu;
import mhl.domain.dining;
import mhl.domain.employee;
import mhl.service.Billservice;
import mhl.service.DiningTbale;
import mhl.service.MenuService;
import mhl.service.employeeservice;

import java.util.List;
import java.util.UUID;

public class MHL_view {
    private boolean loop=true;
    private employeeservice e=new employeeservice();
    private DiningTbale diningTbale=new DiningTbale();
    private MenuService menuService=new MenuService();
    private Billservice billservice=new Billservice();

    private void paybill(){
        System.out.println("请输入要结账的餐桌号(-1退出):");
        int diningTableId=Utility.readInt();
        if (diningTableId==-1){
            System.out.println("取消支付");
            return;
        }
        if (diningTbale.getdiningtable(diningTableId)==null){
            System.out.println("该餐桌不存在");
            return;
        }
        if (!(billservice.haspaybill(diningTableId))){
            System.out.println("该餐桌号没有未结账菜单");
            return;
        }
        System.out.println("请输入支付方式（微信/现金/支付宝,回车取消支付）:");
        String paymode= Utility.readString(20,"");
        if ("".equals(paymode)) {
            System.out.println("取消支付");
            return;

        }
        System.out.println("确定是否支付（Y/N）");
        char key=Utility.readConfirmSelection();
        if (key=='Y') {//结账
            if (billservice.paybill(diningTableId,paymode)){
            System.out.println("支付成功");
            }else {
                System.out.println("支付失败");
            }
        } else {
            System.out.println("取消支付");
        }
    }
    private void listDiningTable(){//显示Table状态
        List<dining> list=diningTbale.getdiningservice();
        for (dining d:list
        ) {
            System.out.println(d);
        }
    }
    private void listMenuTable(){//显示菜单
        List<Menu> list= menuService.getMenuTable();
        System.out.println("\n菜品编号\t\t价格\t\t\t菜类\t\t\t菜名");
        for (Menu d:list
        ) {
            System.out.println(d);
        }
    }
    private void orderBillTable(){
        System.out.println("=======开始点餐=======");
        System.out.println("请输入餐桌号（-1退出）:");
        int orderDiningTable=Utility.readInt(1);
        if (orderDiningTable==-1){
            System.out.println("退出点餐");
            return;
        }
        System.out.println("请输入菜品号（-1退出）:");
        int orderMenu=Utility.readInt(1);
        if (orderMenu==-1){
            System.out.println("退出点餐");
            return;
        }
        System.out.println("请输入菜量:");
        int Nums=Utility.readInt();
        if (Nums==-1){
            System.out.println("退出点餐");
            return;
        }
        dining getdiningtable = diningTbale.getdiningtable(orderDiningTable);
        if (getdiningtable==null){
            System.out.println("=======餐桌不存在=======");
            return;
        }
        Menu menu = menuService.getmenuId(orderMenu);
        if (menu==null){
            System.out.println("=======菜品不存在======");
            return;
        }
        if (billservice.orderBill(orderMenu,Nums,orderDiningTable)) {
            System.out.println("点餐成功");
        } else {
            System.out.println("点餐失败");
        }

    }

    private void orderDiningTable(){//锁定Table
        System.out.println("======输入你想要预定的Table号（输入-1取消预定）:======");
        int orderId=Utility.readInt(1);
        if(orderId==-1){
            System.out.println("=======取消预约成功========");
            return;
        }
        System.out.println("======是否要预约Y/N======");
        char key1=Utility.readConfirmSelection();
        if (key1 == 'Y'){//要预定
            dining diningTbale1=diningTbale.getdiningtable(orderId);
        if (diningTbale1==null){//座位不存在
            System.out.println("========预约失败=======");
            return;
        }
        if (!("空".equals(diningTbale.getdiningtable(orderId).getState()))){//座位已被预约
            System.out.println("========预约失败=======");
            return;
        }

            System.out.println("请输入你的名字:");
            String orderName=Utility.readString(50);
            System.out.println("请输入你的手机号:");
            String Tel=Utility.readString(50);
            diningTbale.getdiningtable1(orderName,Tel,orderId);
            System.out.println("========预约成功=======");
        }else {
            System.out.println("=======取消预约=======");
        }
    }
    public void listBill(){
        List<Bill> list=billservice.getBillTable();
        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
        for (Bill b:list
             ) {
            System.out.println(b);
        }
    }
    public static void main(String[] args) {
        new MHL_view().Mhlmeun();
    }
    public void Mhlmeun(){
        while (loop) {
            System.out.println("==========满汉楼=========");
            System.out.println("\t\t1.登录满汉楼");
            System.out.println("\t\t2.退出满汉楼");
            System.out.println("请输入你的操作：");
            String key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.println("请输入员工号：");
                    String empId=Utility.readString(50);
                    System.out.println("请输入密 码：");
                    String pwd=Utility.readString(50);
                    employee employee= e.getemployeeservice(empId,pwd);
                    if(employee!=null){
                        System.out.println("=======登录成功！======");
                        while (loop){
                        System.out.println("======二级菜单=========");
                        System.out.println("\t\t1.显示餐桌状态");
                        System.out.println("\t\t2.锁定餐桌");
                        System.out.println("\t\t3.显示所有菜品");
                        System.out.println("\t\t4.点餐服务");
                        System.out.println("\t\t5.查看账单");
                        System.out.println("\t\t6.结账");
                        System.out.println("\t\t9.退出满汉楼");
                        System.out.println("请输入你的选择：");
                        key=Utility.readString(1);
                        switch (key){
                            case "1":
                                listDiningTable();
                                break;
                            case "2":
                                orderDiningTable();
                                break;
                            case "3":
                                listMenuTable();
                                break;
                            case "4":
                                orderBillTable();
                                break;
                            case "5":
                                listBill();
                                break;
                            case "6":
                                System.out.println("\t\t6.结账");
                                break;
                            case "9":
                                System.out.println("\t\t9.退出满汉楼");
                                break;
                            default:
                                System.out.println("你的输入有误");
                        }
                        }
                    }else {
                        System.out.println("======登录失败=======");
                    }
                    break;
                case "2":
                    loop=false;
                    break;
                default:
                    System.out.println("你的输入有误");
            }
        }
    }
}
